package com.shengqiang.zhenhuiTransfer.service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.shengqiang.zhenhuiTransfer.entity.Account;
import com.shengqiang.zhenhuiTransfer.entity.AssAcountParams;
import com.shengqiang.zhenhuiTransfer.entity.Result;
import com.shengqiang.zhenhuiTransfer.entity.WeiXinAccountResult;
import com.shengqiang.zhenhuiTransfer.mapper.AccountMapper;
import com.shengqiang.zhenhuiTransfer.utils.WxUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AccountService extends ServiceImpl<AccountMapper,Account>{

    private static final Logger logger = LoggerFactory.getLogger(AccountService.class);

    private Gson gson=new Gson();

    @Autowired
    private WxUtils wxUtils;

    /**
     * 绑定用户
     * @param params
     * @return
     */
    public Result<String> associatedAccount(AssAcountParams params){
        if(params!=null){
            WeiXinAccountResult weixinResult=wxUtils.chekWx(params.getJscode());
            if(weixinResult==null){
                return new Result<>(1,"get weixin result has error");
            }
            Account account=new Account();
            account.setOpenId(weixinResult.getOpenid());
            account.setRealName(params.getRealName());
            account.setPhone(params.getPhone());
            account.setLicensePlate(params.getLicensePlate());
            account.setStatus(1);
            account.setRole("normal");
            account.setCreateTime("2018-05-15 23:56:00");
            return addAccount(account);
        }else {
            return new Result<>(0,"params has error");
        }
    }


    private Result<String> addAccount(Account account){
        try{
            baseMapper.insert(account);
            return new Result<>(0,"success");
        }catch (Exception ex){
            logger.error("添加用户账号出现异常:{},account={}",ex,account);
            return new Result<>(1,"fail");
        }
    }

    public Result<Account> login(String jsCode){
        try{
             WeiXinAccountResult weixinResult=wxUtils.chekWx(jsCode);
            if(weixinResult==null){
                return new Result<>(1,"get weixin result has error");
            }

            String openId=weixinResult.getOpenid();
            Account account=baseMapper.selectById(openId);
            if(account==null){
                return new Result<>(2,"not register in my app");
            }

            Map<String,Object> loginResult= Maps.newHashMap();
            return new Result<>(0,"login success",account);
        }catch (Exception ex){
            logger.error("用户登录出现异常:{}",ex.getMessage(),ex);
            return new Result<>(1,"fail");
        }
    }

    public Result<List<Account>> queryAllAccounts(){
        try{
            List<Account> accounts=baseMapper.queryAllAccounts();
            if(accounts==null){
                return new Result<>(2,"未查询到用户列表");
            }
            return new Result<>(0,"success",accounts);
        }catch (Exception ex){
            logger.error("查询所有用户信息出现异常:{}",ex.getMessage(),ex);
            return new Result<>(1,"fail");
        }
    }
}
